package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.dto.UpdateArticleMessageDto;
import com.heima.behavior.service.IApBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.dto.UserThreadLocalUtil;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : wuqiang
 * @since : 2022-10-18
 */
@Service
public class ApBehaviorServiceImpl implements IApBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(BehaviorDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //当前登录用户
        User user = UserThreadLocalUtil.get();
        if (user == null && user.getUserId() == 0) {
            return ResponseResult.okResult();
        }
        Long articleId = dto.getArticleId();
        Integer authorId = dto.getAuthorId();

        // "isfollow": true,
        Boolean follow = checkByArticleIdOrAuthorId("saveApFollow", user.getUserId(), articleId, authorId);
        // "islike": true,
        Boolean likes = checkByArticleIdOrAuthorId("saveApLikes", user.getUserId(), articleId, null);
        // "isunlike": false,
        Boolean collection = checkByArticleIdOrAuthorId("saveApCollection", user.getUserId(), articleId, null);
        // "iscollection": true
        Boolean unlikes = checkByArticleIdOrAuthorId("saveApUnlikes", user.getUserId(), articleId, null);
        //创建返回结果
        Map resultMap = new HashMap<>();
        resultMap.put("isfollow", follow);
        resultMap.put("islike", likes);
        resultMap.put("isunlike", unlikes);
        resultMap.put("iscollection", collection);
        return ResponseResult.okResult(resultMap);
    }

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Override
    @Async
    public void sendBehaviorMessageToKafka(UpdateArticleMessageDto articleMessage) {
        kafkaTemplate.send("hot_article_score_topic", JSON.toJSONString(articleMessage));
    }


    /**
     * 检查文章id或作者id
     *
     * @param methodName 方法名称
     * @param userId     用户id
     * @param articleId  文章id
     * @param authorId   作者id
     * @return {@link Boolean}
     */
    private Boolean checkByArticleIdOrAuthorId(String methodName, Integer userId, Long articleId, Integer authorId) {
        // methodName + ":" + arg.getUserId() + ":" + arg.getArticleId() + ":" + arg.getAuthorId());
        return redisTemplate.hasKey(methodName + ":" + userId + ":" + articleId + "_" + authorId);
    }


}
